/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Builder;

import DAO.InformationFilter.CategoryDAO;
import DAO.InformationFilter.CategoryPageDAO;
import DAO.InformationFilter.PageDAO;
import DAO.InformationFilter.PeriodClickDAO;
import DAO.InformationFilter.PrdDAO;
import DAO.InformationFilter.PrdTotalPageDAO;
import Info.CategoryPageInfo;
import Info.PageInfo;
import Info.PeriodInfo;
import MySqlAccess.Article;
import MySqlAccess.ArticleInfo;
import MySqlAccess.DataAcessHelper;
import MySqlAccess.RatingInfo;
import MySqlAccess.UserRating;
import java.util.HashMap;
import java.util.List;
import IUtil.Counter;

/**
 *
 * @author tran tuan tai
 */
public class CategoryPage_PeriodBuilder
{
    public void Build()
    {
        PrdDAO period = PrdDAO.GetInstance();
        List<PeriodInfo> allPeriod = period.GetAllPeriod();

        CategoryDAO categoryDAO = new CategoryDAO();
        List<Integer> lstCategory = categoryDAO.GetAllID();

        CategoryPageDAO categoryPageDAO = new CategoryPageDAO();
        categoryPageDAO.DoEmpty();

        PrdTotalPageDAO periodPageDAO = PrdTotalPageDAO.GetInstance();
        periodPageDAO.DoEmpty();

        try
        {
            PageDAO pageDAO = new PageDAO();
            for (PeriodInfo iterPeriod : allPeriod)
            {
                HashMap<Integer, Counter> categoryPage = new HashMap<Integer, Counter>(); //categoryid, counter

                List<PageInfo> lstPage = pageDAO.GetByDate(iterPeriod.GetBegin(), iterPeriod.GetEnd());

                for (Integer currCategory : lstCategory)
                {
                    categoryPage.put(currCategory, new Counter());
                }
                int totalPeriodPage = 0;
                for (Integer currCategory : lstCategory)
                {
                    for (PageInfo pageinfo : lstPage)
                    {
                        if (pageinfo.GetCategory() == currCategory)
                        {
                            Counter counter = categoryPage.get(currCategory);
                            counter.Increment();
                        }
                    }
                    Counter counter = categoryPage.get(currCategory);
                    if (counter != null)
                    {
                        totalPeriodPage += counter.GetCounter();
                    }
                    

                }// end for category

                for (Integer currCategory : lstCategory)
                {
                    CategoryPageInfo info = new CategoryPageInfo();
                    info.category = currCategory;
                    info.period = iterPeriod.GetID();
                    info.totalPage = categoryPage.get(currCategory).GetCounter(); //total page of category
                    if (totalPeriodPage != 0)
                    {
                        info.percentCategory = info.totalPage*1.0/totalPeriodPage;
                    }
                    else
                    {
                        info.percentCategory = 0;
                    }
                    

                    categoryPageDAO.Insert(info);
                }

                periodPageDAO.Insert(iterPeriod.GetID(), totalPeriodPage);
            }// end for period


        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void main(String[] argvs)
    {
        CategoryPage_PeriodBuilder builder = new CategoryPage_PeriodBuilder();
        builder.Build();
    }
}
